using System;
using EnvDTE;

namespace ClassBuilderWizard.Interfaces
{
    public class Common
    {
        private Common()
        {
        }
        /// <summary>
        /// Create an item in the Visual Studio project from a template
        /// </summary>
        /// <param name="contents">The contents of the file after its creation</param>
        /// <param name="parameters">The wizard parameters collected by the Class Builder Wizard</param>
        /// <param name="templateFileName">The name of the template file to use. It should be passed without a file extention.</param>
        /// <param name="createFileName">The name of the file to create. It should be passed without a file extension.</param>
        public static void CreateFileFromTemplate(string contents, Parameters parameters, string templateFileName, string createFileName)
        {
            string prefix = string.Empty;
            string suffix = string.Empty;
            if (parameters.CurrentProjectType == ProjectType.CSharp)
            {
                //the project is C#
                prefix = @"\..\VC#\VC#Wizards\CSharpClassBuilderWiz";
                suffix = (templateFileName.StartsWith("file")) ? "cs" : "sql";
            }
            else
            {
                //the project is VB.NET
                prefix = @"\..\VB\VBWizards\VBClassBuilderWiz";
                suffix = (templateFileName.StartsWith("file")) ? "vb" : "sql";
            }

            //create the file stream for reading the teamplate
            string templatefile = parameters.IDEInstallPath + prefix + string.Format(@"\Templates\1033\{0}.{1}", templateFileName, suffix);

            string newItemName = createFileName + "." + suffix;
            EnvDTE.ProjectItem theItem = parameters.IDEProjectItems.AddFromTemplate(templatefile, newItemName);
            Window newDoc = theItem.Open(Constants.vsViewKindCode);

            newDoc.Document.ReplaceText("[!output CLASS_DEFINITION]", contents, 0);
            newDoc.Document.ReplaceText("[!output SQL_SCRIPT]", contents, 0);
            newDoc.Activate();
            //format the document with proper indentation
            if (suffix != "sql")
                parameters.IDEApplication.ExecuteCommand("Edit.FormatDocument", "");
        }

        /// <summary>
        /// Check to see if a file exists in the Visual Studio project.
        /// </summary>
        /// <param name="filename">The filename (including file extention), to look for</param>
        /// <param name="parameters">The wizard parameters collected by the Class Builder Wizard</param>
        /// <returns>Returns the ProjectItem if it is found. Otherwise, null.</returns>
        public static ProjectItem FindFileInProject(string filename, Parameters parameters)
        {
            foreach (ProjectItem pi in parameters.IDEProjectItems)
            {
                if (pi.get_FileNames(0).EndsWith(filename))
                    return pi;
            }

            return null;
        }
    }
}
